﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.StopZero.CApStopZeroApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.StopZero.Resources;
using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Tecnomatix.Engineering;
using UiInternalControls;


namespace DnProcessSimulateCommands.StopZero;

public class CApStopZeroApp : INotifyPropertyChanged
{
  private const double DefaultSamplingTimeInterval = 0.1;
  private readonly TxColor _selectedSpheresColor = new TxColor((byte) 204, (byte) 51, (byte) 0);
  private CApStopZeroLocationOrOperationValidator _startLocationValidator;
  private CApStopZeroLocationOrOperationValidator _endLocationValidator;
  private CApStopZeroSpheresValidator _spheresValidator;
  private CApStopZeroScopeValidator _resultContainerValidator;
  private List<CApStopZeroApp.StopTypeComboItem> _stopTypeItems;
  private ITxRoboticOperation _startLocationOrOperation;
  private ITxRoboticLocationOperation _startLocation;
  private ITxRoboticOperation _endLocationOrOperation;
  private ITxRoboticLocationOperation _endLocation;
  private ITxRobot _robot;
  private ITxObject _gun;
  private bool _isSpheresSelectionEnabled;
  private ITxObjectCollection _spheresContainer;
  private List<ITxObject> _spheres;
  private CApStopZeroEmphasizer _shperesEmphasizer;
  private bool _accuracyMode;
  private double _samplingTimeInterval;
  private StopTypes _stopType;
  private ITxComponent _resultContainer;
  private double _progress;
  private string _statusMessage;
  private bool _isCreationPossible;
  private bool _isIdle;
  private bool _isGenerationStopped;

  public CApStopZeroApp()
  {
    this.InitializeValidators();
    this._spheres = new List<ITxObject>();
    this._shperesEmphasizer = new CApStopZeroEmphasizer(this._selectedSpheresColor, (TxColorEmphasizePriority) 200);
    this._accuracyMode = true;
    this._samplingTimeInterval = 0.1;
    this._stopType = StopTypes.StopZero;
    this._statusMessage = string.Empty;
    this._stopTypeItems = new List<CApStopZeroApp.StopTypeComboItem>();
    this._stopTypeItems.Add(new CApStopZeroApp.StopTypeComboItem(StopTypes.StopZero, StopZeroStringTable.StopZero));
    this._stopTypeItems.Add(new CApStopZeroApp.StopTypeComboItem(StopTypes.StopTwoRamp, StopZeroStringTable.StopTwoRamp));
    this._isIdle = true;
  }

  public event PropertyChangedEventHandler PropertyChanged;

  public ITxRoboticOperation StartLocationOrOperation
  {
    get => this._startLocationOrOperation;
    set
    {
      if (this._startLocationOrOperation == value)
        return;
      if (value is ITxRoboticOrderedCompoundOperation operation)
      {
        Tuple<ITxRoboticLocationOperation, ITxRoboticLocationOperation> locations = this.StartLocationValidator.GetLocations(operation);
        this.StartLocation = locations.Item1;
        this.EndLocationOrOperation = (ITxRoboticOperation) locations.Item2;
      }
      else
        this.StartLocation = value as ITxRoboticLocationOperation;
      this._startLocationOrOperation = (ITxRoboticOperation) this.StartLocation;
      this.RaisePropertyChanged(nameof (StartLocationOrOperation));
    }
  }

  private ITxRoboticLocationOperation StartLocation
  {
    get => this._startLocation;
    set
    {
      if (this._startLocation == value)
        return;
      this._startLocation = value;
      this.SetRobotAndGun();
      this.SetIsGenerationPossible();
    }
  }

  public ITxRoboticOperation EndLocationOrOperation
  {
    get => this._endLocationOrOperation;
    set
    {
      if (this._endLocationOrOperation == value)
        return;
      if (value is ITxRoboticOrderedCompoundOperation operation)
      {
        Tuple<ITxRoboticLocationOperation, ITxRoboticLocationOperation> locations = this.EndLocationValidator.GetLocations(operation);
        this.EndLocation = locations.Item2;
        this.StartLocationOrOperation = (ITxRoboticOperation) locations.Item1;
      }
      else
        this.EndLocation = value as ITxRoboticLocationOperation;
      this._endLocationOrOperation = (ITxRoboticOperation) this.EndLocation;
      this.RaisePropertyChanged(nameof (EndLocationOrOperation));
    }
  }

  public ITxRoboticLocationOperation EndLocation
  {
    get => this._endLocation;
    set
    {
      if (this._endLocation == value)
        return;
      this._endLocation = value;
      this.SetRobotAndGun();
      this.SetIsGenerationPossible();
    }
  }

  public ITxRobot Robot
  {
    get => this._robot;
    private set
    {
      if (this._robot == value)
        return;
      this._robot = value;
      this.RaisePropertyChanged(nameof (Robot));
    }
  }

  public ITxObject Gun
  {
    get => this._gun;
    private set
    {
      if (this._gun == value)
        return;
      this._gun = value;
      this.RaisePropertyChanged(nameof (Gun));
      this.IsSpheresSelectionEnabled = this._gun != null;
      this.ClearShperes();
    }
  }

  public bool IsSpheresSelectionEnabled
  {
    get => this._isSpheresSelectionEnabled;
    private set
    {
      if (this._isSpheresSelectionEnabled == value)
        return;
      this._isSpheresSelectionEnabled = value;
      this.RaisePropertyChanged(nameof (IsSpheresSelectionEnabled));
    }
  }

  public ITxObjectCollection SpheresContainer
  {
    get => this._spheresContainer;
    set
    {
      if (this._spheresContainer == value)
        return;
      this._spheresContainer = value;
      this.RaisePropertyChanged(nameof (SpheresContainer));
      this.SetSpheres(this._spheresContainer);
    }
  }

  public List<ITxObject> Spheres
  {
    get => this._spheres;
    set
    {
      if (!this._spheres.IsDifferent<ITxObject>((IEnumerable<ITxObject>) value))
        return;
      this._spheres = value;
      this._shperesEmphasizer.SetEmphasizedItems(this._spheres);
      this.SetIsGenerationPossible();
    }
  }

  [TxRegistrySerializable]
  public double SamplingTimeInterval
  {
    get => this._samplingTimeInterval;
    set
    {
      if (this._samplingTimeInterval == value)
        return;
      this._samplingTimeInterval = value;
      this.RaisePropertyChanged(nameof (SamplingTimeInterval));
    }
  }

  [TxRegistrySerializable]
  public bool AccuracyMode
  {
    get => this._accuracyMode;
    set
    {
      if (this._accuracyMode == value)
        return;
      this._accuracyMode = value;
      this.RaisePropertyChanged(nameof (AccuracyMode));
    }
  }

  [TxRegistrySerializable]
  public StopTypes StopType
  {
    get => this._stopType;
    set
    {
      if (this._stopType == value)
        return;
      this._stopType = value;
      this.RaisePropertyChanged(nameof (StopType));
    }
  }

  public List<CApStopZeroApp.StopTypeComboItem> StopTypeItems => this._stopTypeItems;

  public ITxComponent ResultContainer
  {
    get => this._resultContainer;
    set
    {
      if (this._resultContainer == value)
        return;
      this._resultContainer = value;
      this.RaisePropertyChanged(nameof (ResultContainer));
      this.SetIsGenerationPossible();
    }
  }

  public double Progress
  {
    get => this._progress;
    set
    {
      if (this._progress == value)
        return;
      this._progress = value;
      this.RaisePropertyChanged(nameof (Progress));
    }
  }

  public string StatusMessage
  {
    get => this._statusMessage;
    set
    {
      if (this._statusMessage == value)
        return;
      this._statusMessage = value;
      this.RaisePropertyChanged(nameof (StatusMessage));
    }
  }

  public bool IsCreationPossible
  {
    get => this._isCreationPossible;
    private set
    {
      if (this._isCreationPossible == value)
        return;
      this._isCreationPossible = value;
      this.RaisePropertyChanged(nameof (IsCreationPossible));
    }
  }

  public bool IsIdle
  {
    get => this._isIdle;
    private set
    {
      if (this._isIdle == value)
        return;
      this._isIdle = value;
      this.RaisePropertyChanged(nameof (IsIdle));
    }
  }

  public CApStopZeroLocationOrOperationValidator StartLocationValidator
  {
    get => this._startLocationValidator;
  }

  public CApStopZeroLocationOrOperationValidator EndLocationValidator => this._endLocationValidator;

  public CApStopZeroScopeValidator ResultContainerValidator => this._resultContainerValidator;

  public CApStopZeroSpheresValidator SpheresValidator => this._spheresValidator;

  public void Create()
  {
    this.IsIdle = false;
    new CApStopZeroMotionVolumeCreator(this.StartLocation, this.EndLocation, (ICollection<ITxObject>) this.Spheres, this.AccuracyMode, this.SamplingTimeInterval, (int) this._stopType, this._selectedSpheresColor, this.ResultContainer as ITxGeometryCreation, (Action<CApStopZeroProgressReporterData>) (progressData => this.UpdateProgress(progressData)), (Func<bool>) (() => this._isGenerationStopped)).Create();
    if (!this._isGenerationStopped)
      this.Clear();
    this._isGenerationStopped = false;
    this.IsIdle = true;
  }

  public void Clear()
  {
    this.StartLocationOrOperation = (ITxRoboticOperation) null;
    this.EndLocationOrOperation = (ITxRoboticOperation) null;
    this.ClearShperes();
    this.ResultContainer = (ITxComponent) null;
  }

  public void Stop() => this._isGenerationStopped = true;

  private void UpdateProgress(CApStopZeroProgressReporterData progressData)
  {
    if (progressData == null)
      return;
    this.Progress = progressData.CurrentProgress;
    string str = string.Empty;
    if (progressData.CurrentProgress > 0.0)
    {
      str = StopZeroStringTable.InitalizationPhase;
      if (progressData.Initialized)
      {
        if (progressData.CurrentLocation != null)
          str = string.Format(StopZeroStringTable.SamplingPhase, (object) ((ITxObject) progressData.CurrentLocation).Name);
        if (progressData.GeneratingVolume)
          str = StopZeroStringTable.GeneratingVolumePhase;
      }
    }
    this.StatusMessage = str;
  }

  private void SetSpheres(ITxObjectCollection spheresContainer)
  {
    this.Spheres = this._spheresValidator.GetScopeSpheres(spheresContainer);
  }

  private void ClearShperes() => this.SpheresContainer = (ITxObjectCollection) null;

  private void SetRobotAndGun()
  {
    this.Robot = CApStopZeroUtilities.GetAssignedRobot(this.StartLocation ?? this.EndLocation);
    this.Gun = CApStopZeroUtilities.GetActiveGun(this.StartLocation ?? this.EndLocation);
    if (this.IsSpheresSelectionEnabled)
      return;
    List<ITxObject> activeTools = CApStopZeroUtilities.GetActiveTools(this.StartLocation ?? this.EndLocation);
    this.IsSpheresSelectionEnabled = activeTools != null && activeTools.Count > 0;
  }

  private void InitializeValidators()
  {
    this._startLocationValidator = new CApStopZeroLocationOrOperationValidator((Func<ITxRoboticLocationOperation>) (() => this.EndLocation));
    this._endLocationValidator = new CApStopZeroLocationOrOperationValidator((Func<ITxRoboticLocationOperation>) (() => this.StartLocation));
    this._resultContainerValidator = new CApStopZeroScopeValidator();
    this._spheresValidator = new CApStopZeroSpheresValidator(new Func<List<ITxLocatableObject>>(this.GetActiveToolsToValidate), new Func<List<ITxLocatableObject>>(this.GetMountedToolsToValidate), new Func<ITxRobot>(this.GetRobotToValidate));
  }

  private ITxRobot GetRobotToValidate() => this.Robot;

  private List<ITxLocatableObject> GetMountedToolsToValidate()
  {
    List<ITxLocatableObject> itxLocatableObjectList = new List<ITxLocatableObject>();
    return CApStopZeroUtilities.GetMountedTools(this.StartLocation ?? this.EndLocation).ConvertAll<ITxLocatableObject>((Converter<ITxObject, ITxLocatableObject>) (x => x as ITxLocatableObject));
  }

  private List<ITxLocatableObject> GetActiveToolsToValidate()
  {
    List<ITxLocatableObject> activeToolsToValidate = new List<ITxLocatableObject>();
    if (this.Gun != null)
      activeToolsToValidate.Add(this.Gun as ITxLocatableObject);
    else
      activeToolsToValidate = CApStopZeroUtilities.GetActiveTools(this.StartLocation ?? this.EndLocation).ConvertAll<ITxLocatableObject>((Converter<ITxObject, ITxLocatableObject>) (x => x as ITxLocatableObject));
    return activeToolsToValidate;
  }

  private void SetIsGenerationPossible()
  {
    this.IsCreationPossible = this.StartLocation != null && this.EndLocation != null && this.Spheres != null && this.Spheres.Count > 0 && this.ResultContainer != null;
  }

  private void RaisePropertyChanged(string caller)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(caller));
  }

  public class StopTypeComboItem
  {
    public StopTypeComboItem(StopTypes type, string name)
    {
      this.Name = name;
      this.Type = type;
    }

    public StopTypes Type { get; private set; }

    public string Name { get; private set; }
  }
}
